import pygame, sys, math, random

pygame.init()

from Ball import Ball           
from button import Button
from Player import Player
from Heart import Heart
from HealthPack import HealthPack

clock = pygame.time.Clock()

width = 1000
height = 550
size = width, height

screen = pygame.display.set_mode(size)

bgImage = pygame.image.load("RSC/background/black.png")
bgImage = pygame.transform.scale(bgImage, (width, height))
bgRect = bgImage.get_rect()


startButton = Button("RSC/buttons/START.png", [width/2, height/2], [200,100])


balls = []
powerups = []
pygame.mixer.music.load("RSC/monster.mp3")
       


while True:
    start = False
    while not start:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == pygame.MOUSEBUTTONDOWN:
                if startButton.collidePoint(event.pos):
                    start = True
                    hearts = [Heart([25,25]),
                              Heart([50,25]),
                              Heart([75,25])]
                    player = Player([width/2, height-75])
                    
        
        screen.blit(bgImage, bgRect)
        screen.blit(startButton.image,startButton.rect)
        #screen.blit(optionsButton.image,optionsButton.rect)
        pygame.display.flip()
        clock.tick(60)
    
    level = 1
    pygame.mixer.music.play()
    score = 0
    while start and player.living:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_d or event.key == pygame.K_RIGHT:
                    player.direction("right")
                if event.key == pygame.K_a or event.key == pygame.K_LEFT:
                    player.direction("left")
                if event.key == pygame.K_SPACE:
                    player.jump()
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_d or event.key == pygame.K_RIGHT:
                    player.direction("stop right")
                if event.key == pygame.K_a or event.key == pygame.K_LEFT:
                    player.direction("stop left")
                if event.key == pygame.K_SPACE:
                    player.stopJump()
        
        font = pygame.font.Font("RSC/buttons/Molot.otf", 36)
        text = font.render("Score: "+ str(score), 1, (45, 153, 255))
        textpos = text.get_rect(centerx = width/2)
        
        if random.randint(0,37) == 0:
            bsize = random.randint(25, 150)
            xspeed = 0
            while xspeed == 0:
                xspeed = random.randint(-2,2)
            balls += [Ball([xspeed, random.randint(1,5)], 
                        [bsize, bsize], 
                        [random.randint(75, width-75),0])]
                        
        if random.randint(0,60*60) == 0:
            bsize = 25
            xspeed = random.randint(-2,2)
            powerups += [HealthPack([xspeed, random.randint(1,5)], 
                        [bsize, bsize], 
                        [random.randint(75, width-75),0])]
        
        for ball in balls:
            ball.update()
        for powerup in powerups:
            powerup.update()
        player.update()     
        
        for ball in balls:
            ball.collideWall(width, height)
        for powerup in powerups:
            powerup.collideWall(width, height)
        player.collideWall(width, height)
        
        for ball in balls:
            if player.collideBall(ball):
                heart.lose()
        for powerup in powerups:
            if player.collideBall(powerup):
                if powerup.type == "HealthPack":
                    hearts = [Heart([25,25]),
                              Heart([50,25]),
                              Heart([75,25])]
        
        if len(balls) > 1:
           for first in range(len(balls)-1):
               for second in range(first+1,len(balls)):
                   balls[first].collideBall(balls[second])
        
        for ball in balls:
            if not ball.living:
                score+=1
                balls.remove(ball)
                
        for powerup in powerups:
            if not powerup.living:
                powerups.remove(powerup)
                
        for heart in hearts:
            if not heart.living:
                hearts.remove(heart)
                
        if len(hearts) == 0:
            player.living = False

        screen.blit(bgImage, bgRect)
        screen.blit(player.image, player.rect)
        for ball in balls:
            screen.blit(ball.image, ball.rect)
        for powerup in powerups:
            screen.blit(powerup.image, powerup.rect)
        for heart in hearts:
            screen.blit(heart.image, heart.rect)
        screen.blit(text, textpos)
        pygame.display.flip()
        clock.tick(60) 